/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package clinique;

import com.mysql.jdbc.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

/**
 *
 * @author rachid
 */
@WebService()
public class WebServiceClinique
{
    @WebMethod(operationName = "Authentication")
    public String[] Authentication(@WebParam(name = "username")
    String username)
    {
       String[] userData = new String[20];
        String user = "root";
        String password = "";
        String url = "jdbc:mysql://localhost/app_clinique";

        try
        {
             Connection connexion;
             Statement statement = null;

             // Chargement du driver mysql
             Class.forName("com.mysql.jdbc.Driver").newInstance();

             // Connexion à la base de donnée clinique
             connexion = (Connection) DriverManager.getConnection(url,user,password) ;

             if (connexion != null)
             {
                 String query = "SELECT U.id, U.username, U.algorithm, U.salt, U.password, U.created_at, U.last_login, U.is_active, U.is_super_admin " +
                                "FROM sf_guard_user U " +
                                "WHERE U.username LIKE \"" + username + "\" ;";

                 System.out.println(query);
                 statement = (Statement) connexion.createStatement();
                 ResultSet rs = (ResultSet) statement.executeQuery(query);

                 while (rs.next())
                 {
                    userData[0] = rs.getString("id");
                    userData[1] = rs.getString("username");
                    userData[2] = rs.getString("algorithm");
                    userData[3] = rs.getString("salt");
                    userData[4] = rs.getString("password");
                    userData[5] = rs.getString("created_at");
                    userData[6] = rs.getString("last_login");
                    userData[7] = rs.getString("is_active");
                    userData[8] = rs.getString("is_super_admin");


                    //System.out.println(userData.toString());
                 }

                 // Fermeture des resources
                 statement.close();
                 connexion.close();
             }
        }
        catch(Exception ex)
        {
           System.out.println("Exception : " + ex.toString());
        }

    return userData;
    }

}
